<template>
    <el-card class="page">

        <!-- 标题 -->
        <template #header>
            <div class="header">
                <span>妆造师信息</span>
                <div>
                    <el-button type="primary" @click="visibleDrawer = true,clearPhotoInfo()">添加妆造师</el-button>
                </div>
            </div>
        </template>

        <!-- 摄影师搜索 -->
        <el-form inline>
            <el-form-item label="妆造师检索：">
                <el-input placeholder="请输入妆造师姓名" style="width: 200px;" v-model="queryInput"></el-input>
            </el-form-item>

            <!-- 搜索-->
            <el-form-item>
                <el-button type="primary" @click="getPhoto">搜索</el-button>
                <el-button @click="queryInput = ''">重置</el-button>
            </el-form-item>
        </el-form>


        <!-- 文章列表 -->
        <el-table style="width: 100%;" :data="photo"    :row-class-name="tableRowClassName">
            <el-table-column label="编号" prop="id"></el-table-column>
            <el-table-column label="姓名" prop="name"></el-table-column>
            <el-table-column label="联系方式" prop="phone"></el-table-column>
            <el-table-column label="状态" prop="version"></el-table-column>


            <el-table-column label="照片">
                <template #default="{ row }">
                    <img v-if="row.pic" :src="row.pic" alt="图片" style="width: 50px; height: 50px;">
                    <el-avatar v-else shape="square" :size="50">无封面</el-avatar>
                </template>
            </el-table-column>


            <!-- 操作按钮 -->
            <el-table-column label="操作" width="200">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showDialogData(row)"></el-button>
                    <el-button :icon="SwitchButton" circle plain type="danger" @click="open(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>


        <!-- 分页 -->
        <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5, 10, 15]"
            layout="jumper, total, sizes, pager, next" @current-change="onCurrentChange" @size-change="onSizeChange"
            :total="total" background style="margin-top: 20px;justify-content: flex-end;" />



        <!-- 搭建抽屉组件 -->
        <el-drawer v-model="visibleDrawer" title="注册妆造师" size="40%">
            <!-- 添加文章表单 -->
            <!-- label-width 定义所有表单标签 el-form-item 的宽度 -->
            <el-form :model="photoInfo" label-width="100px" :rules="rules">

                <el-form-item label="名称" prop="name">
                    <el-input v-model="photoInfo.name" placeholder="请输入名称"></el-input>
                </el-form-item>

                <el-form-item label="联系方式" prop="phone">
                    <el-input v-model="photoInfo.phone" placeholder="请输入联系方式"></el-input>
                </el-form-item>

                <!-- 图片 -->
                <el-form-item label="妆造师头像">
                    <el-upload class="avatar-uploader" :show-file-list="false" :auto-upload="true"
                        action="http://localhost:8080/photo/uploadAliOss" name="file" :on-success="uploadSuccess">

                        <img v-if="photoInfo.pic" :src="photoInfo.pic" class="avater">
                        <el-icon v-else class="avatar-uploader-icon">
                            <Plus />
                        </el-icon>
                    </el-upload>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" plain @click="registerPhoto">注册</el-button>
                </el-form-item>


                <img src="../../images/user-head.jpg" width="400px" height="250px"
                    style="padding: 8px 10px; border: 1px dashed #4e6ef2;margin-left: 100px;margin-top: 40px;border-radius: 8px;">
            </el-form>
        </el-drawer>

        <!-- 修改弹窗 -->
        <el-dialog v-model="dialogFormVisible" title="修改妆造师信息" width="600" style="margin-top: 170px;">
            <el-form :model="photoInfo" label-width="100px" :rules="rules">

                <el-form-item label="名称" prop="name">
                    <el-input v-model="photoInfo.name" placeholder="请输入名称"></el-input>
                </el-form-item>

                <el-form-item label="联系方式" prop="phone">
                    <el-input v-model="photoInfo.phone" placeholder="请输入联系方式"></el-input>
                </el-form-item>

                <!-- 图片 -->
                <el-form-item label="摄影师头像">
                    <el-upload class="avatar-uploader" :show-file-list="false" :auto-upload="true"
                        action="http://localhost:8080/photo/uploadAliOss" name="file" :on-success="uploadSuccess">

                        <img v-if="photoInfo.pic" :src="photoInfo.pic" class="avater">
                        <el-icon v-else class="avatar-uploader-icon">
                            <Plus />
                        </el-icon>
                    </el-upload>
                </el-form-item>

            </el-form>

            <template #footer>
                <div>
                    <el-button
                        @click="dialogFormVisible = false, clearPhotoInfo(), ElMessage.info('已取消')">取消修改</el-button>
                    <el-button type="primary" @click="updatePhoto">确认修改</el-button>
                </div>
            </template>
        </el-dialog>
    </el-card>
</template>

<script setup>
import { ref } from 'vue';
import makeApi from '@/api/make'
import photoApi from '@/api/photo'
import { Edit, SwitchButton, Plus } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus'

const dialogFormVisible = ref(false)
//控制抽屉是否显示
const visibleDrawer = ref(false)
//检索数据
const queryInput = ref('');

const photoInfo = ref({
    name: '',
    phone: '',
    pic: ''
})

//妆造师据模型
const photo = ref([]);

//分页数据模型
const pageNum = ref(1); //当前页
const pageSize = ref(5);//每页条数
const total = ref(10)   //总条数




//每页条数发生变化，自动调用此函数
const onSizeChange = (size) => {
    pageSize.value = size
    getPhoto()
}

//页码发生变化，自动调用此函数
const onCurrentChange = (num) => {
    pageNum.value = num
    getPhoto()
}


const getPhoto = async () => {
    //扩展数据模型
    let data = {
        pageNum: pageNum.value,
        pageSize: pageSize.value,
        queryInput: queryInput.value ? queryInput.value : null
    }
    const res = await makeApi.getMakeByPage(data)
    if (res.data.code === 0) {
        total.value = res.data.data.total
        photo.value = res.data.data.records
    }

}
getPhoto()

const rules = {
    name: [
        //元素失去焦点时
        { required: true, message: "请输入称呼", trigger: 'blur' },
        { min: 1, max: 10, message: '都说了1~10位???', trigger: 'blur' }
    ],
    phone: [
        { required: true, message: "请输入11位手机号码", trigger: 'blur' },
        { min: 11, max: 11, message: '11位，11位，怎么回事啊???', trigger: 'blur' }
    ],
}

const uploadSuccess = async (res) => {
    //如果有值删掉
    if (photoInfo.value.pic) {
        //如果当前添加的图片有值，那就说明，那就要把之前的删掉
        await photoApi.deleteAliyunPhoto(photoInfo.value.pic)
    }
    //为空正常赋值 让上传的图片显示
    console.log(res.data)
    photoInfo.value.pic = res.data
}

//清空数据模型
const clearPhotoInfo = () => {
    photoInfo.value.name = ''
    photoInfo.value.phone = ''
    photoInfo.value.pic = ''
}

//注册妆造师
const registerPhoto = async () => {
    if (photoInfo.value.phone.length == 11 && photoInfo.value.name.length >= 1) {
        const res = await makeApi.addMake(photoInfo.value)
        if (res.data.code === 0) {
            ElMessage.success("注册成功")
            //刷新列表
            getPhoto()
            clearPhotoInfo()
            visibleDrawer.value = false
        }
    } else {
        ElMessage.error("请输入正确的信息")
    }

}

const showDialogData = (row) => {

    //数据添加id
    photoInfo.value.id = row.id
    photoInfo.value.name = row.name
    photoInfo.value.phone = row.phone
    photoInfo.value.pic = row.pic

    //打开弹窗
    dialogFormVisible.value = true
}

//修改
const updatePhoto = async () => {
    if (photoInfo.value.phone.length == 11 && photoInfo.value.name.length >= 1) {
       makeApi.updateMake(photoInfo.value).then((res) => {
            if (res.data.code === 0) {
                ElMessage.success("修改成功")
                //刷新列表
                getPhoto()
                clearPhotoInfo()
                dialogFormVisible.value = false
            }
        })
    } else {
        ElMessage.error("请输入正确的信息")
    }
}

const open = (info) => {
    ElMessageBox.confirm(
        '是否切换状态！请确认！',
        '提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            //删除员工函数回调
            deletePhoto(info)
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '以取消操作',
            })
        })
}

//id对象  
const data = ref({
    id:"",
})

//删除妆造师
const deletePhoto = async (info) => {
    data.value.id = info.id
    const res = await makeApi.deleteMake(data.value.id)
    if (res.data.code == 0) {
        ElMessage.success("状态修改成功")
        //刷新列表
        getPhoto()
    }
}


import { toRaw } from 'vue';

const tableRowClassName = ({ row }) => {
  const rawRow = toRaw(row); // 获取 Proxy 的目标对象
  if (rawRow.version === '离职') {
    return 'warning-row';
  }
  return '';
};
</script>



<style scoped>
:deep(.el-table .warning-row) {
  --el-table-tr-bg-color: var(--el-color-warning-light-9) !important;
  background-color: var(--el-color-warning-light-9) !important; /* 直接设置背景颜色 */
}
.page {
    min-height: 100%;
    box-sizing: border-box;
}

.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-upload img {
    width: 178px;
    height: 178px;
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
    border: 1px solid gainsboro;
}
</style>